/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package access;

import dto.FolderMapping;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author LW
 */
public class FileAccess implements Serializable {


    public boolean exists(String path){
        File f = new File(path);
        if(!f.exists()) return false;

        return true;
    }

    /**
     * write to file
     * @param pathFolderToSave full path
     * @param fileName file name
     * @throws FileNotFoundException
     * @throws IOException
     */
    public boolean writeToFile(String fileName, FolderMapping fm){
        try{
            String fp = null;
            fp = "mapping/" + fileName;
            FileOutputStream fos = new FileOutputStream(fp);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            //write this
            oos.writeObject(fm);
            oos.close();

            return true;
        }catch(IOException ex){
            return false;
        }
    }

    /**
     * return folder path from folder mapping, or null if file not exists
     * @param fileName
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public String getFolder(String fileName) throws FileNotFoundException, IOException, ClassNotFoundException{        
        //check exists file
        if(!exists(fileName)) return null;

        //else read mapping file
        FileInputStream fins = new FileInputStream(fileName);
        ObjectInputStream ois = new ObjectInputStream(fins);
        Object obj = ois.readObject();
        FolderMapping fm = null;
        if(obj instanceof FolderMapping){
            fm = (FolderMapping) obj;
            //get the path
            return fm.getPath();
        }else{
            //if not foldermapping file
            return null;
        }
    }

    public long fileLength(String filePath) throws FileNotFoundException{
        //check exists file
        if(!exists(filePath)) return 0;
        
        File f = new File(filePath);
        return f.length();
    }

    /**
     * get the folder mapping file
     * @return
     */
    public FolderMapping[] getListFile(){
        File folder = new File("mapping/");
        File[] fileList = folder.listFiles();
        FolderMapping[] list = new FolderMapping[fileList.length];
        //loop the file list
        int temp = 0;
        for(int i = 0; i < fileList.length; i++){
            if(fileList[i].isFile()){
                try{
                    FileInputStream fis = new FileInputStream(fileList[i]);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    try {
                        //check the folder mapping
                        Object obj = ois.readObject();
                        if(obj instanceof FolderMapping){ //is foldermapping
                            list[temp] = (FolderMapping)obj;
                        }
                        ois.close();
                        fis.close();
                    } catch (ClassNotFoundException ex) {
                        //not found folder mapping class
                        return null;
                    }
                    
                }catch(IOException ex){}                
            }
            temp++;
        }
        return list;
    }

    /**
     * delete file in mapping folder
     * @param fileName
     */
    public void deleteFile(String fileName){
        File f = new File("mapping/"+fileName);
        boolean temp = f.canWrite();

        f.delete();
    }
}
